package com.xnx3.wangmarket.admin.service.impl;

import com.xnx3.j2ee.dao.SqlDAO;
import com.xnx3.wangmarket.admin.entity.EmpiricalConclusion;
import com.xnx3.wangmarket.admin.entity.Page;
import com.xnx3.wangmarket.admin.entity.Product;
import com.xnx3.wangmarket.admin.entity.SiteColumn;
import com.xnx3.wangmarket.admin.service.EmpiricalConclusionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.xnx3.j2ee.shiro.ShiroFunc.getUserId;

@Service("EmpiricalConclusionImpl")
public class EmpiricalConclusionImpl implements EmpiricalConclusionService {
    @Resource
    private SqlDAO sqlDAO;

    @Override
    public void updateEmpirical(EmpiricalConclusion empiricalConclusion, int userid, HttpServletRequest request) {
        sqlDAO.save(empiricalConclusion);
    }

    @Override
    public List<SiteColumn> empiricalType(int userid) {
        return sqlDAO.findBySqlQuery("select b.* from site_column a join site_column b on a.id=b.parentid and a.userid=" + userid + " and a.type=7", SiteColumn.class);
    }

    @Override
    public SiteColumn pctType(int id) {
        return sqlDAO.findById(SiteColumn.class, id);
    }

    @Override
    public List<EmpiricalConclusion> queryEmpiricalList(int userid) {
        return sqlDAO.findBySqlQuery("select * from EmpiricalConclusion where user_id =" + userid, EmpiricalConclusion.class);
    }

    @Override
    public EmpiricalConclusion queryEmpirical(int id) {
        return sqlDAO.findById(EmpiricalConclusion.class, id);
    }

    @Override
    public void delEmpirical(EmpiricalConclusion empiricalConclusion) {
        sqlDAO.delete(empiricalConclusion);
    }

    @Override
    public List<EmpiricalConclusion> empiricalListPage(int page, int count, int userid, EmpiricalConclusion empiricalConclusion) {
        if (empiricalConclusion.getName() != null) {
            return sqlDAO.findBySqlQuery("select * from empirical_conclusion where user_id = " + userid + " and name like " + "'%" + empiricalConclusion.getName() + "%'" + " ORDER BY site_column_id LIMIT " + ((page - 1) * 10) + " , " + count, EmpiricalConclusion.class);
        }
        if (empiricalConclusion.getSiteColumnId() != null) {
            return sqlDAO.findBySqlQuery("select * from empirical_conclusion where user_id = " + userid + " and site_column_id = " + empiricalConclusion.getSiteColumnId() + " ORDER BY site_column_id LIMIT " + ((page - 1) * 10) + " , " + count, EmpiricalConclusion.class);
        }
        if (empiricalConclusion.getName() != null && empiricalConclusion.getSiteColumnId() != null) {
            return sqlDAO.findBySqlQuery("select * from empirical_conclusion where user_id = " + userid + " and name = " + empiricalConclusion.getName() + " and site_column_id= " + empiricalConclusion.getSiteColumnId() + " ORDER BY site_column_id LIMIT " + ((page - 1) * 10) + " , " + count, EmpiricalConclusion.class);
        }
        if (empiricalConclusion.getName() == null && empiricalConclusion.getSiteColumnId() == null) {
            return sqlDAO.findBySqlQuery("select * from empirical_conclusion where user_id = " + userid + " ORDER BY site_column_id LIMIT " + ((page - 1) * 10) + " , " + count, EmpiricalConclusion.class);
        }
        return null;
    }

    @Override
    public int empiricalCount(int userid) {
        List<EmpiricalConclusion> list = sqlDAO.findBySqlQuery("select * from empirical_conclusion where user_id=" + userid, EmpiricalConclusion.class);
        return list.size() + 1;
    }

    @Override
    public Page findPage(int page, int count, EmpiricalConclusion empiricalConclusion) {

        List<EmpiricalConclusion> ecList = empiricalListPage(page, count, getUserId(), empiricalConclusion);
        int totle = empiricalCount(getUserId());
        Page p = new Page();
        p.setEcList(ecList);
        p.setCurrentPage(page);
        p.setCount(count);
        p.setTotalCount(totle);
        int totlePage = totle % count == 0 ? totle / count : (totle / count) + 1;
        p.setTotalPage(totlePage);

        return p;
    }
}
